package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SpZu;
import com.ruoyi.system.mapper.SpXiangmuMapper;
import com.ruoyi.system.domain.SpXiangmu;
import com.ruoyi.system.service.ISpXiangmuService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-10
 */
@Service
public class SpXiangmuServiceImpl implements ISpXiangmuService 
{
    @Autowired
    private SpXiangmuMapper spXiangmuMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询项目
     * 
     * @param xmId 项目主键
     * @return 项目
     */
    @Override
    public SpXiangmu selectSpXiangmuByXmId(Long xmId)
    {
        return spXiangmuMapper.selectSpXiangmuByXmId(xmId);
    }

    /**
     * 查询项目列表
     * 
     * @param spXiangmu 项目
     * @return 项目
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpXiangmu> selectSpXiangmuList(SpXiangmu spXiangmu)
    {


        return spXiangmuMapper.selectSpXiangmuList(spXiangmu);
    }

    @Override
    public List<SpXiangmu> selectSpXiangmuListWeb(SpXiangmu spXiangmu)
    {
        return spXiangmuMapper.selectSpXiangmuList(spXiangmu);
    }

    /**
     * 新增项目
     * 
     * @param spXiangmu 项目
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSpXiangmu(SpXiangmu spXiangmu)
    {
        spXiangmu.setCreateTime(DateUtils.getNowDate());
        int rows = spXiangmuMapper.insertSpXiangmu(spXiangmu);
//        if(spXiangmu.getCtids()!=null){
//            String ctids=spXiangmu.getCtids()+","+spXiangmu.getXmId();
//            List<String> ctidList = Arrays.asList(ctids.split(","));
//            ctidList =ctidList.stream().distinct().collect(Collectors.toList());
//            for(String xmid:ctidList){
//                if(xmid!=null&&!"".equals(xmid.trim())){
//                    spXiangmuMapper.selectSpXiangmuByXmId(Long.parseLong(xmid));
//                }
//            }
//
//        }

        insertSpZu(spXiangmu);
        return rows;
    }

    /**
     * 修改项目
     * 
     * @param spXiangmu 项目
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSpXiangmu(SpXiangmu spXiangmu)
    {
        spXiangmuMapper.deleteSpZuByXmId(spXiangmu.getXmId());
        insertSpZu(spXiangmu);
        return spXiangmuMapper.updateSpXiangmu(spXiangmu);
    }

    /**
     * 批量删除项目
     * 
     * @param xmIds 需要删除的项目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpXiangmuByXmIds(Long[] xmIds)
    {
        spXiangmuMapper.deleteSpZuByXmIds(xmIds);
        return spXiangmuMapper.deleteSpXiangmuByXmIds(xmIds);
    }

    /**
     * 删除项目信息
     * 
     * @param xmId 项目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpXiangmuByXmId(Long xmId)
    {
        spXiangmuMapper.deleteSpZuByXmId(xmId);
        return spXiangmuMapper.deleteSpXiangmuByXmId(xmId);
    }

    /**
     * 新增分组信息信息
     * 
     * @param spXiangmu 项目对象
     */
    public void insertSpZu(SpXiangmu spXiangmu)
    {
        List<SpZu> spZuList = spXiangmu.getSpZuList();
        Long xmId = spXiangmu.getXmId();
        if (StringUtils.isNotNull(spZuList))
        {
            List<SpZu> list = new ArrayList<SpZu>();
            for (SpZu spZu : spZuList)
            {
                spZu.setXmId(xmId);
                list.add(spZu);
            }
            if (list.size() > 0)
            {
                spXiangmuMapper.batchSpZu(list);
            }
        }
    }
}
